package com.guli.edu.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guli.common.ResultCodeEnum;
import com.guli.common.exception.GuliException;
import com.guli.common.vo.R;
import com.guli.edu.entity.Course;
import com.guli.edu.entity.Teacher;
import com.guli.edu.query.CourseQuery;
import com.guli.edu.query.CourseWebQuery;
import com.guli.edu.query.TeacherQuery;
import com.guli.edu.service.ChapterService;
import com.guli.edu.service.CourseService;
import com.guli.edu.vo.ChapterVo;
import com.guli.edu.vo.CourseWebVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author dxd
 * @since 2019-12-09
 */
@RestController
@RequestMapping("/eduservice/frontCourse")
@CrossOrigin //跨域
@Api(tags = "课程模块")
public class CourseController {
    @Autowired
    private CourseService courseService;

    @ApiOperation(value = "分页课程列表")
    @GetMapping(value = "{page}/{limit}")
    public R pageList(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,

            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit) {

        Page<Course> pageParam = new Page<Course>(page, limit);

        Map<String, Object> map = courseService.pageListWeb(pageParam);

        return R.ok().data(map);
    }

    @Autowired
    private ChapterService chapterService;

    @ApiOperation(value = "根据ID查询课程")
    @GetMapping(value = "{courseId}")
    public R getById(
            @ApiParam(name = "courseId", value = "课程ID", required = true)
            @PathVariable String courseId) {

        //查询课程信息和讲师信息
        CourseWebVo courseWebVo = courseService.selectInfoWebById(courseId);

        //查询当前课程的章节信息
        List<ChapterVo> chapterVoList = chapterService.nestedList(courseId);

        return R.ok().data("course", courseWebVo).data("chapterVoList", chapterVoList);
    }

    //分页条件查询
    @ApiOperation(value = "分页讲师列表")
    @GetMapping("pageQuery/{page}/{limit}")
    public R pageQuery(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,

            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,

            @PathVariable Integer id) {
        //CourseWebQuery courseWebQuery
        if (page <= 0 || limit <= 0) {
            //throw new GuliException(21003, "参数不正确1");
            throw new GuliException(ResultCodeEnum.PARAM_ERROR);
        }
        if (id == 1) {
            //
            return R.ok();
        } else if (id == 2) {
            return R.ok();
        } else if (id == 3) {
            return R.ok();
        }

        return R.ok();
    }
}

